package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.model.User;
import com.service.UserService;

@RestController
public class Controller {
	
	@Autowired //将自动引入studentService
    private UserService userService;
	
	@RequestMapping(value = "/index")
	public ModelAndView index() {
		ModelAndView modelAndView = new ModelAndView("welcome");
		modelAndView.addObject("loginName", "Peter");
		return modelAndView;
	}
	
	@RequestMapping(value = "/login",method=RequestMethod.POST)
	public ModelAndView login(@RequestParam("userName") String userName,@RequestParam("userPwd") String userPwd) {
		
		//身份验证
		User user = userService.findByName(userName).get(0);
		if(user !=null && user.getPwd().equals(userPwd) )
		{			
			ModelAndView modelAndView = new ModelAndView("welcome");
			modelAndView.addObject("loginName", "userName");
			return modelAndView;
		}
		else
		{
			ModelAndView modelAndView = new ModelAndView("login");
			return modelAndView;
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
